(function() {
    $.fn.ui_select = function(data, name, typeid) {
        $.fn.ui_select.data = data;
        return this.each(function() {
            $this = $(this);
            initselect($this, name, typeid);
            initevent($this);
        });
    };

    function initselect($this, name, typeid) {
        $this.html('<div class="ui-select"><div>请选择分类<i></i></div><span></span><input name="' + name + '" type="hidden" value="'+ typeid +'" /></div>');
    }

    function initevent($this) {
        var $select = $this.find(".ui-select");
        var $div = $select.find("div");
        var $span = $select.find("span");
        var $input = $select.find("input");

        $div.on("click", function() {
            var select_id = $input.val().split("-");
            var type_html = "";
            for (var i = 0; i < select_id.length; i++) {
                if (i == 0)
                    type_html += createoption($.fn.ui_select.data["0"], select_id[i]);
                else
                    type_html += createoption($.fn.ui_select.data[select_id[i - 1]], select_id[i]);
            }
            $span.html(type_html);

            if ($select.hasClass("active")) {
                $span.slideUp(200, function() {
                    $select.removeClass("active");
                });
            } else {
                $select.addClass("active");
                $span.slideDown(200);
            }
            return false;
        });

        $span.on("mouseenter", "li", function() {
            $(this).siblings().removeClass("active");
            $(this).addClass("active");
            var typeid = $(this).attr('data-id');
            $(this).parent().nextAll().remove("ul");
            $(this).parent().after(createoption($.fn.ui_select.data[typeid], ''));
        }).on("click", "li", function() {
            var type = "";
            var typeid = "";
            $span.find("li.active").each(function() {
                type += $(this).text() + " > ";
                typeid += $(this).attr('data-id') + '-';
            });
            var tex = type.slice(0, -3);
            var val = typeid.slice(0, -1);
            $div.html(tex + "<i></i>");
            $input.val(val);

            $.get('/get_filter/upload/' + val.substr(val.lastIndexOf('-')+1),function(data){
                $this.parent().next().html(data);
            });

            $.fn.ui_select.val = val;
            $.fn.ui_select.text = tex;
        });

        $(document).delegate("body", "click", function() {
            $span.slideUp(200, function() {
                $select.removeClass("active");
            });
        });
    }

    //根据active的select_id,显示type_json数组数据
    function createoption(type_json, select_id) {
        if (type_json == undefined)
            return "";
        var type_html = '<ul>';
        for (var i = 0; i < type_json.length; i++) {
            if (type_json[i]["typeid"] == select_id) {
                type_html += "<li class='active' data-id='" + type_json[i]['typeid'] + "'>" + type_json[i]['typename'] + "</li>";
            } else {
                type_html += "<li data-id='" + type_json[i]['typeid'] + "'>" + type_json[i]['typename'] + "</li>";
            }
        }
        return type_html + "</ul>";
    }

})(jQuery);